﻿
#region License, Terms and Conditions
//
// BugHerderAccountRetrieverSection.cs
//
// Authors: Kori Francis <twitter.com/djbyter>
// Copyright (C) 2012 Kori Francis. All rights reserved.
// 
// THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#endregion

namespace BugHerder.Core.Configuration
{
    #region Imports
    using System.Configuration;
    #endregion

    /// <summary>
    /// Class that deals with getting the account elements in web/app.config
    /// </summary>
    public class BugHerderAccountRetrieverSection : ConfigurationSection
    {   
        /// <summary>
        /// The name of the default account, to be used in GetDefaultOrFirst() method.
        /// </summary>
        [ConfigurationProperty("defaultAccount")]
        public string DefaultAccount
        {
            get { return (string)base["defaultAccount"]; }
            set { base["defaultAccount"] = value; }
        }

        // This will be necessary eventually, when they release json along side xml for API consumption
        ///// <summary>
        ///// Should BugHerder use JSON or XML. UseJSON = false by default (ie XML by default)
        ///// </summary>
        //[ConfigurationProperty("useJSON")]
        //public bool UseJSON
        //{
        //    get 
        //    {
        //        if (base["useJSON"] != null) { return (bool)base["useJSON"]; }
        //        else { return false; }
        //    }
        //    set { base["useJSON"] = value; }
        //}

        /// <summary>
        /// The collection of BugHerd Account elements
        /// </summary>
        [ConfigurationProperty("accounts", IsDefaultCollection=true)]
        public BugHerderAccountElementCollection Accounts
        {
            get { return (BugHerderAccountElementCollection)this["accounts"]; }
            set { this["accounts"] = value; }
        }

        /// <summary>
        /// Method that gets the default (as specified via the DefaultAccount property) or the first
        /// </summary>
        public BugHerderAccountElement GetDefaultOrFirst()
        {
            BugHerderAccountElement result = null;
            if (!string.IsNullOrEmpty(this.DefaultAccount))
            {
                foreach (BugHerderAccountElement element in this.Accounts)
                {
                    if (element.Name == this.DefaultAccount)
                    {
                        result = element;
                        break;
                    }
                }
            }
            else
            {
                // If there are account elements in the web.config then ..
                if (this.Accounts.Count > 0)
                {
                    result = this.Accounts[0] as BugHerderAccountElement;
                }
            }

            if (result == null) { throw new ConfigurationErrorsException("No accounts listed for BugHerder in the .config configuation file."); }
            else
            {
                return result;
            }
        }
    }
}
